package com.rfid.emergencywarehouse.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.adapter.KwAdapter;
import com.rfid.emergencywarehouse.adapter.ScanBarcodeAdapter;
import com.rfid.emergencywarehouse.entity.BarcodeInfo;
import com.rfid.emergencywarehouse.entity.KW;
import com.rfid.emergencywarehouse.entity.Order;
import com.rfid.emergencywarehouse.presenter.ShelfingWithOrderPresenter;
import com.rfid.emergencywarehouse.view.IOnShelfWithOrderView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ShelfingWithOrderFragment extends ShelfingFragment<ShelfingWithOrderPresenter> implements IOnShelfWithOrderView {

    @BindView(R.id.kf)
    TextView mKf;

    @BindView(R.id.order_id)
    TextView mOrderNo;

    @BindView(R.id.kw_recyclerView)
    RecyclerView mKwRecycler;

    KwAdapter mKwAdapter;
    List<KW> mKWS = new ArrayList<>();
    String orderId;
    String toWorkAreaId;
    @Override
    public ShelfingWithOrderPresenter getPresenter() {
        return new ShelfingWithOrderPresenter();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("有单上架");
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mKwAdapter = new KwAdapter(mKWS);
        mKwRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        mKwRecycler.setAdapter(mKwAdapter);
        Bundle bundle = getArguments();
        mKf.setText("库房：" + bundle.getString("kf"));
        mOrderNo.setText("单号：" + bundle.getString("orderNo"));
        orderId = bundle.getString("orderId");
        mPresenter.getOrderKws(orderId);

        toWorkAreaId = bundle.getString("toWorkAreaId","");
        mPresenter.postEnterStatus(toWorkAreaId,orderId);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.exitWorkArea(toWorkAreaId,orderId);
    }

    @Override
    public void onShelfingResult(boolean succes, String msg) {
        super.onShelfingResult(succes, msg);
        if (succes) {
            mPresenter.getOrderKws(orderId);
        }
    }

    @Override
    public void onOrderList(List<Order> orders) {
    }

    @Override
    public void onRecommendKws(List<KW> kws) {
        System.out.println("onRecommendKws = " + kws);
        mKWS.clear();
        mKWS.addAll(kws);
        mKwAdapter.notifyDataSetChanged();
    }
}
